package data;

import java.io.IOException;

import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.tags.OptionTag;
import org.htmlparser.tags.SelectTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;

import restlet.interfaces.PlaceReseurceI;

import beans.MestoBean;

public class AddPlaces {

	public static void main(String[] args) throws ResourceException,
	IOException {

		//final String URL = "localhost:8888"; 
		final String URL = "idemdo.appspot.com"; 
		
		ClientResource cr = new ClientResource("http://" + URL + "/rest/places");
		PlaceReseurceI placeResource = cr.wrap(PlaceReseurceI.class);
		System.out.println("Saljemo mesta...");
		
		System.out.println("Poslat: Novi Sad!");
		placeResource.addPlace(new MestoBean("NOVI SAD"));
		
		try {
			System.out.println("\n\tUcitavamo pocetnu stranicu...");
			Parser parser = new Parser("http://gspns.rs/redvoznje.php?rv=rvm");
			System.out.println("\n\tStranica ucitana!");
			// parsiramo stranicu
			NodeList nl = parser.parse(new HasAttributeFilter("name","linija[]"));
			Tag tag = (Tag) nl.elementAt(0);

			// izvlacimo gradove iz stranice
			SelectTag slGradovi = (SelectTag) tag;
			// System.out.println("Ukupno gradova: "+slGradovi.getChildCount());
			OptionTag otGradovi[] = slGradovi.getOptionTags();
			String[] gradovi = new String[otGradovi.length];

			System.out.println("Ucitano: " + gradovi.length + " mesta");
			
			for (int i = 0; i < otGradovi.length; i++) {
				gradovi[i] = otGradovi[i].getValue();
				System.out.println("(" + i + ")" + "Dodajemo: " + gradovi[i]);
				placeResource.addPlace(new MestoBean(gradovi[i]));
			}		
			
			// Novi Sad moramo da dodamo rucno jer, ga nema u listi
		} catch (ParserException e) {
			e.printStackTrace();
		}

		System.out.println("Uspesno poslata mesta");
	}
}
